package com.cooba.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cooba.R;
import com.cooba.adapter.GameAdapter;
import com.cooba.application.MyApplication;
import com.cooba.bean.GameBean;
import com.cooba.bean.GameDetailBean;
import com.cooba.utils.ACache;
import com.cooba.utils.CarURL;
import com.cooba.utils.CommonUtil;
import com.cooba.utils.ServerURL;
import com.cooba.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/20.
 * 新闻详情
 */
public class GameDetailsActivity extends AppCompatActivity {

    private ImageButton back;
    private RelativeLayout container_game;
    private TextView game_desc;
    private RelativeLayout container_like;
    private ViewPager game_viewpager;
    private ViewPager like_viewpager;
    private ImageView game_icon;
    private TextView game_title;
    private TextView game_size;
    private TextView game_integral;
    private RatingBar game_star;
    private TextView game_download;

    private HttpUtils httpUtils;
    private RequestParams params;
    private String dir_name;
    private String game_id;
    private Boolean isIncar;
    private String mac;
    private List<String> game_image_list = new ArrayList<>();
    private MyPagerAdapter adapter;
    private GameDetailBean gameDetailBean;
    private List<GameDetailBean.GameDetailInfo> gameList = new ArrayList<>();
    private MyPagerAdapter1 adapter1;
    private BitmapUtils bitmapUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_details);

        PushAgent.getInstance(this).onAppStart();

        bitmapUtils = new BitmapUtils(this);
        dir_name = getIntent().getStringExtra("dir_name");
        game_id = getIntent().getStringExtra("game_id");
        initIncar();
        initView();
        getGameDetail();
    }

    private void initView() {
        back = (ImageButton) findViewById(R.id.game_image_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        game_icon = (ImageView) findViewById(R.id.game_detail_icon);
        game_title = (TextView) findViewById(R.id.game_detail_name);
        game_size = (TextView) findViewById(R.id.game_detail_size);
        game_star = (RatingBar) findViewById(R.id.game_detail_star);
        game_integral = (TextView) findViewById(R.id.game_detail_integral);
        game_desc = (TextView) findViewById(R.id.game_desc);
        game_download = (TextView) findViewById(R.id.game_detail_download);
        game_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //CommonUtil.downloadAPK(this,gameDetailBean.);
            }
        });
        container_game = (RelativeLayout) findViewById(R.id.container1);
        container_like = (RelativeLayout) findViewById(R.id.container2);
        game_desc = (TextView) findViewById(R.id.game_desc);
        game_viewpager = (ViewPager) findViewById(R.id.game_view_pager1);
        game_viewpager.setOffscreenPageLimit(5);
        game_viewpager.setPageMargin(10);
        MyOnPageChangeListener myOnPageChangeListener = new MyOnPageChangeListener();
        game_viewpager.setOnPageChangeListener(myOnPageChangeListener);

        container_game.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // dispatch the events to the ViewPager, to solve the problem that we can swipe only the middle view.
                return game_viewpager.dispatchTouchEvent(event);
            }
        });


        like_viewpager = (ViewPager) findViewById(R.id.game_view_pager2);
        like_viewpager.setOffscreenPageLimit(8);
        like_viewpager.setPageMargin(10);
        MyOnPageChangeListener1 myOnPageChangeListener1 = new MyOnPageChangeListener1();
        like_viewpager.setOnPageChangeListener(myOnPageChangeListener1);
        container_like.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // dispatch the events to the ViewPager, to solve the problem that we can swipe only the middle view.
                return like_viewpager.dispatchTouchEvent(event);
            }
        });
    }

    private void initIncar() {
        MyApplication application = MyApplication.getMyApplication();
        isIncar = application.getIsInCar();
        mac = application.getMac();
    }

    private void getGameDetail() {
        String urlgamedetail;
        httpUtils = new HttpUtils();
        params = new RequestParams();
        params.addBodyParameter("dir_name", dir_name);
        params.addBodyParameter("id", game_id);
        if (isIncar) {
            urlgamedetail = CarURL.gameDetail;
        } else {
            urlgamedetail = ServerURL.httpUrl + ServerURL.gameDetail;
        }
        httpUtils.send(HttpRequest.HttpMethod.POST, urlgamedetail, params, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Log.e("wei", "获取游戏详情失败");

            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e("wei", "获取游戏详情成功" + responseInfo.result);
                try {
                    gameDetailBean = new Gson().fromJson(responseInfo.result, GameDetailBean.class);
                    if (gameDetailBean.getStatus().equals("1")) {
                        initData();
                    }
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initData() {
        game_image_list.add(gameDetailBean.getGame_file1());
        game_image_list.add(gameDetailBean.getGame_file2());
        game_image_list.add(gameDetailBean.getGame_file3());
        game_image_list.add(gameDetailBean.getGame_file4());
        game_image_list.add(gameDetailBean.getGame_file5());
        adapter = new MyPagerAdapter(this, game_image_list);
        game_viewpager.setAdapter(adapter);
        gameList = gameDetailBean.getGameList();
        adapter1 = new MyPagerAdapter1(this, gameList);
        like_viewpager.setAdapter(adapter1);
        bitmapUtils.display(game_icon, gameDetailBean.getGame_icon());
        game_title.setText(gameDetailBean.getGame_name());
        game_size.setText(gameDetailBean.getGame_size()+"M");
        game_integral.setText(gameDetailBean.getGame_vantager());
        float star;
        if (TextUtils.isEmpty(gameDetailBean.getGame_star())) {
            star = 5;
        } else {
            star = Float.valueOf(gameDetailBean.getGame_star());
        }
        game_star.setRating(star);
        String desc=Utils.replace(gameDetailBean.getGame_desc());
        desc=Utils.htmlFilter(desc);
        game_desc.setText(desc);
    }

    class MyPagerAdapter extends PagerAdapter {
        private List<String> urlList;
        private Context mContext;

        public MyPagerAdapter(Context context, List<String> urlList) {
            this.urlList = urlList;
            mContext = context;
        }

        public void setData(List<String> data) {
            urlList = data;
            notifyDataSetChanged();
        }

        @Override
        public float getPageWidth(int position) {
            return (float)0.5;
        }

        @Override
        public int getCount() {
            return urlList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(GameDetailsActivity.this);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            //imageView.setImageResource(R.drawable.guide1);
            bitmapUtils.display(imageView, urlList.get(position));
            ((ViewPager) container).addView(imageView, position);
            return imageView;

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }
    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // to refresh frameLayout
            if (container_game != null) {
                container_game.invalidate();
            }
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

    class MyPagerAdapter1 extends PagerAdapter {
        private List<GameDetailBean.GameDetailInfo> gameList;
        private Context mContext;

        public MyPagerAdapter1(Context context, List<GameDetailBean.GameDetailInfo> gameList) {
            this.gameList = gameList;
            mContext = context;
        }

        public void setData(List<GameDetailBean.GameDetailInfo> data) {
            gameList = data;
            notifyDataSetChanged();
        }

        @Override
        public float getPageWidth(int position) {
            return (float)0.25;
        }

        @Override
        public int getCount() {
            return gameList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            ImageView imageView = new ImageView(GameDetailsActivity.this);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            //imageView.setImageResource(R.drawable.guide1);
            bitmapUtils.display(imageView, gameList.get(position).getGame_icon());
            imageView.setTag(position);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String gameid = null;
                    String dir_name=null;
                    if (position < gameList.size()) {
                        gameid =gameList.get(position).getId();
                        dir_name=gameList.get(position).getSort_id();
                    }
                    Intent intent = new Intent(GameDetailsActivity.this, GameDetailsActivity.class);
                    intent.putExtra("game_id", gameid);
                    intent.putExtra("dir_name", dir_name);
                    startActivity(intent);
                }
            });
            ((ViewPager) container).addView(imageView, position);
            return imageView;

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }
    }

    public class MyOnPageChangeListener1 implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // to refresh frameLayout
            if (container_like != null) {
                container_like.invalidate();
            }
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
